/*
 * @(#)Environment.java	1.0 Apr 18, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.io.console;

import java.util.concurrent.*;
import java.util.*;
import java.util.regex.*;
import java.io.File;
import java.io.IOException;

public class Environment extends ConcurrentHashMap<String,String>
{
//	protected static final String VAR_FMT = "\\$%s";
	protected static final String VAR_FMT = "{%s}";
	protected static final Pattern VAR_PATTERN = Pattern.compile("\\{([\\w]*?)\\}");
//	protected static final String VAR_PATTERN = "\\Q" + VAR_FMT + "\\E";

	public Environment()
	{
		super();
		reset();
	}

	public void put(String key, File file)
	{
		try{
			put(key, file.getCanonicalPath().replaceAll("\\\\", "/"));
		} catch(IOException e){
			put(key, file.getAbsolutePath());
		}
	}

	public String get(String key)
	{
		String value = super.get(key);
		if(value == null) return value;

		Matcher matcher = VAR_PATTERN.matcher(value);
		while(matcher.find())
		{
			String group = matcher.group();
			String subkey = group.substring(1, group.length()-1);
			String subvalue = get(subkey);
			if(subvalue == null)
				subvalue = "";

			value = value.replaceAll("\\Q" + group + "\\E", subvalue);
		}

//		boolean done = false;
//		while(!done)
//		{
//			boolean found = false;
//			for(String subkey : super.keySet())
//			{
//				String subvalue = super.get(subkey);
//				if(subvalue == null) continue;
//				if(value.indexOf('{') == -1) break;
//				value = value.replaceAll(String.format(VAR_PATTERN, subkey), subvalue);
//				found = true;
//			}
//
//			// keep looping until we have exhausted all possible keys inside of keys
//			if(!found) done = true;
//		}

//		Matcher matcher = VAR_PATTERN.matcher(value);
//		ArrayList<String> found = new ArrayList<String>();
//		while(matcher.find())
//			found.add(matcher.group());
//
//		for(String find : found)
//		{
//			String subvalue = get(find);
//			if(subvalue == null) continue;
//			value = value.replaceAll(String.format(VAR_FMT, find), subvalue);
//		}

		return value;
	}

	public void reset()
	{
		putAll(System.getenv());
//		putAll(System.getProperties());
		Properties p = System.getProperties();
		for(Object key : p.keySet())
			put(key.toString(), p.get(key).toString());
	}

	public ProcessBuilder createProcess(List<String> command)
	{
		ProcessBuilder pb = new ProcessBuilder(command);
		Map<String,String> env = pb.environment();
		env.clear();
		env.putAll(this);
		return pb;
	}

	public ProcessBuilder createProcess(String ... command)
	{
		ProcessBuilder pb = new ProcessBuilder(command);
		Map<String,String> env = pb.environment();
		env.clear();
		env.putAll(this);
		return pb;
	}
}
